package com.audi.activity;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ScrollView;
import android.widget.TextView;

import com.audi.R;

public class DeviceDashboardActivity extends
			Activity {
	public class DeviceType {
		public int idDeviceType;
		public String deviceTypeIcon;
		public String deviceTypeName;
		public int numberOfDevice;

		public DeviceType(int idDeviceType,
					String deviceTypeIcon,
					String deviceTypeName,
					int numberOfDevice) {
			this.idDeviceType = idDeviceType;
			this.deviceTypeIcon = deviceTypeIcon;
			this.deviceTypeName = deviceTypeName;
			this.numberOfDevice = numberOfDevice;
		}
	}

	public class DeviceItem {
		public int idDeviceItem;
		public String deviceName;
		public String devicePlace;
		public String capability;
		public String addedDate;
		public int idDeviceType;

		public DeviceItem(int idDeviceItem,
					String deviceName,
					String devicePlace,
					String capability,
					String addedDate,
					int idDeviceType) {
			this.idDeviceItem = idDeviceItem;
			this.deviceName = deviceName;
			this.devicePlace = devicePlace;
			this.capability = capability;
			this.addedDate = addedDate;
			this.idDeviceType = idDeviceType;
		}
	}

	public ExpandableListView onOffExpandableList,
				warningExpandableList,
				statusExpandableList;
	public ScrollView scrollView;

	private ArrayList<DeviceType> listDeviceTypeOnOff = 
				new ArrayList<DeviceDashboardActivity.DeviceType>();
	private ArrayList<DeviceType> listDeviceTypeWarning = 
				new ArrayList<DeviceDashboardActivity.DeviceType>();
	private ArrayList<DeviceType> listDeviceTypeStatus = 
				new ArrayList<DeviceDashboardActivity.DeviceType>();
	private Hashtable<Integer, ArrayList<DeviceItem>> hashtableDeviceItem = 
				new Hashtable<Integer, ArrayList<DeviceDashboardActivity.DeviceItem>>();

	@Override
	protected void
				onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_device_dashboard);
		initActionBarDashBoard(R.layout.action_bar_dashboard);
		dumdataDashboard();
		onOffExpandableList = (ExpandableListView) findViewById(R.id.expandableListOnOf);
		warningExpandableList = (ExpandableListView) findViewById(R.id.expandableListWarningSensor);
		statusExpandableList = (ExpandableListView) findViewById(R.id.expandableListStatusSensor);
		scrollView = (ScrollView) findViewById(R.id.scrollViewDashboard);
		onOffExpandableList
					.setGroupIndicator(null);
		warningExpandableList
					.setGroupIndicator(null);
		statusExpandableList
					.setGroupIndicator(null);
		// onOffExpandableList.setDivider(devider);
		// onOffExpandableList.setChildDivider(devider);
		onOffExpandableList.setDividerHeight(1);
		onOffExpandableList.setDividerHeight(1);
		statusExpandableList.setDividerHeight(1);

		onOffExpandableList
					.setAdapter(new OnOffExpandableListAdapter(
								DeviceDashboardActivity.this,
								listDeviceTypeOnOff,
								hashtableDeviceItem,
								onOffExpandableList));
		warningExpandableList
					.setAdapter(new OnOffExpandableListAdapter(
								DeviceDashboardActivity.this,
								listDeviceTypeWarning,
								hashtableDeviceItem,
								warningExpandableList));
		statusExpandableList
					.setAdapter(new OnOffExpandableListAdapter(
								DeviceDashboardActivity.this,
								listDeviceTypeStatus,
								hashtableDeviceItem,
								statusExpandableList));
		onOffExpandableList
					.setScrollContainer(false);
		warningExpandableList
					.setScrollContainer(false);
		statusExpandableList
					.setScrollContainer(false);
		setListViewHeightBasedOnChildren(onOffExpandableList);
		setListViewHeightBasedOnChildren(warningExpandableList);
		setListViewHeightBasedOnChildren(statusExpandableList);
	}

	public static void
				setListViewHeightBasedOnChildren(ExpandableListView listView) {
		ListAdapter listAdapter = listView
					.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter
					.getCount(); i++) {
			View listItem = listAdapter.getView(
						i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem
						.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView
					.getLayoutParams();
		params.height = totalHeight
					+ (listView.getDividerHeight() * (listAdapter
								.getCount() - 1));
		listView.setLayoutParams(params);
		listView.requestLayout();
	}

	public void dumdataDashboard() {
		listDeviceTypeOnOff.add(new DeviceType(1,
					"", "Plug", 2));
		listDeviceTypeOnOff.add(new DeviceType(2,
					"", "%device_type%", 1));
		listDeviceTypeWarning.add(new DeviceType(
					3, "", "CO", 2));
		listDeviceTypeWarning.add(new DeviceType(
					4, "", "%sensor_type%", 2));
		listDeviceTypeStatus.add(new DeviceType(
					5, "", "CO", 2));
		listDeviceTypeStatus.add(new DeviceType(
					6, "", "%sensor_type%", 2));
		ArrayList<DeviceItem> temp = new ArrayList<DeviceDashboardActivity.DeviceItem>();
		temp.add(new DeviceItem(1, "Fan plug1",
					"Living room", "Public",
					"On April 4, 2014", 1));
		temp.add(new DeviceItem(2, "Fan plug2",
					"Sleep room", "Public",
					"On April 4, 2014", 1));
		hashtableDeviceItem.put(1, temp);
		temp = new ArrayList<DeviceDashboardActivity.DeviceItem>();
		temp.add(new DeviceItem(3, "Fan plug3",
					"Living room", "Private",
					"On April 4, 2014", 2));
		hashtableDeviceItem.put(2, temp);
		temp = new ArrayList<DeviceDashboardActivity.DeviceItem>();
		temp.add(new DeviceItem(4, "Fan plug4",
					"Living room", "Public",
					"On April 4, 2014", 3));
		temp.add(new DeviceItem(5, "Fan plug5",
					"Living room", "Public",
					"On April 4, 2014", 3));
		hashtableDeviceItem.put(3, temp);
		hashtableDeviceItem.put(4, temp);
		hashtableDeviceItem.put(5, temp);
		hashtableDeviceItem.put(6, temp);
	}

	public class OnOffExpandableListAdapter
				extends BaseExpandableListAdapter {

		private Context context;
		private ArrayList<DeviceType> listDeviceType = 
					new ArrayList<DeviceDashboardActivity.DeviceType>();
		private Hashtable<Integer, ArrayList<DeviceItem>> hashDeviceItem = 
					new Hashtable<Integer, ArrayList<DeviceItem>>();
		private ExpandableListView expanlist;

		public OnOffExpandableListAdapter(
					Context context,
					ArrayList<DeviceType> parent,
					Hashtable<Integer, ArrayList<DeviceItem>> child,
					ExpandableListView expanlist) {
			this.context = context;
			this.listDeviceType = parent;
			this.hashDeviceItem = child;
			this.expanlist = expanlist;
		}

		@Override
		public Object getChild(int groupPosition,
					int childPosititon) {
			return hashDeviceItem
						.get(listDeviceType
									.get(groupPosition).idDeviceType)
						.get(childPosititon);
		}

		@Override
		public long getChildId(int groupPosition,
					int childPosition) {
			return childPosition;
		}

		@Override
		public View
					getChildView(int groupPosition,
								final int childPosition,
								boolean isLastChild,
								View convertView,
								ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) this.context
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater
							.inflate(R.layout.row_child_dashboard,
										null);
			}
			TextView childName = (TextView) convertView
						.findViewById(R.id.dashboard_child_name);
			TextView childPlace = (TextView) convertView
						.findViewById(R.id.dashboard_child_place);
			TextView childCapability = (TextView) convertView
						.findViewById(R.id.dashboard_child_capability);
			TextView childAddDate = (TextView) convertView
						.findViewById(R.id.dashboard_child_add_date);
			DeviceItem itemDevice = (DeviceItem) getChild(
						groupPosition,
						childPosition);
			if (itemDevice != null) {
				childName.setText(itemDevice.deviceName);
				childPlace.setText(itemDevice.devicePlace);
				childCapability
							.setText(itemDevice.capability);
				childAddDate.setText(itemDevice.addedDate);
			}
			return convertView;
		}

		@Override
		public int
					getChildrenCount(int groupPosition) {
			return hashDeviceItem
						.get(listDeviceType
									.get(groupPosition).idDeviceType)
						.size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return listDeviceType
						.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return listDeviceType.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View
					getGroupView(int groupPosition,
								boolean isExpanded,
								View convertView,
								ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) this.context
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater
							.inflate(R.layout.row_parent_dashboard,
										null);
			}
			TextView farentTitle = (TextView) convertView
						.findViewById(R.id.dashboard_farent_name);
			TextView farentCount = (TextView) convertView
						.findViewById(R.id.dashboard_farent_count);
			ImageView farentIsExpand = (ImageView) convertView
						.findViewById(R.id.dashboard_farent_is_expand);
			farentTitle.setText(listDeviceType
						.get(groupPosition).deviceTypeName);
			farentCount.setText(listDeviceType
						.get(groupPosition).numberOfDevice
						+ " "
						+ getString(R.string.count_device));
			if (isExpanded) {
				farentIsExpand
							.setImageResource(R.drawable.dashboard_up_expanlist);
			} else {
				farentIsExpand
							.setImageResource(R.drawable.dashboard_down_expanlist);
			}
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public void
					onGroupCollapsed(int groupPosition) {
			// LinearLayout.LayoutParams param = (LinearLayout.LayoutParams)
			// expanlist
			// .getLayoutParams();
			// param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
			// expanlist.setLayoutParams(param);
			// expanlist.refreshDrawableState();
			setListViewHeightBasedOnChildren(expanlist);
			scrollView.refreshDrawableState();
		}

		@Override
		public void
					onGroupExpanded(int groupPosition) {
			// LinearLayout.LayoutParams param = (LinearLayout.LayoutParams)
			// expanlist
			// .getLayoutParams();
			// param.height = (hashDeviceItem.get(
			// listDeviceType.get(groupPosition).idDeviceType).size() *
			// expanlist
			// .getHeight());
			// expanlist.setLayoutParams(param);
			// expanlist.refreshDrawableState();
			setListViewHeightBasedOnChildren(expanlist);
			scrollView.refreshDrawableState();
		}

		@Override
		public boolean
					isChildSelectable(int groupPosition,
								int childPosition) {
			return true;
		}
	}

	protected void
				initActionBarDashBoard(int actionBarDashboardView) {
		ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
					.inflate(actionBarDashboardView,
								null);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setCustomView(
					actionBarLayout,
					new ActionBar.LayoutParams(
								ViewGroup.LayoutParams.MATCH_PARENT,
								ViewGroup.LayoutParams.MATCH_PARENT));
		TextView backDashboard = (TextView) actionBarLayout
					.findViewById(R.id.txt_dashboard_back);
		backDashboard
					.setOnClickListener(new OnClickListener() {

						@Override
						public void
									onClick(View arg0) {
							finish();
						}
					});
	}
}
